package com.dashu.device.controller.JLServer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dashu.base.utils.http.RequestWrapper;
import com.dashu.device.bean.JL.JLHeartBean;
import com.dashu.device.result.ResultBean;
import com.dashu.device.service.JL.JLInsideInsideService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;


/**
 * @Author xys
 * @Date 2020/11/17 15:21
 */

@RequestMapping("dashu/device/server/JL")
@RestController
@Slf4j
@Api(value = "dashu/device/server/JL", tags = {"设备服务模块"}, description = "设备")
public class JLDeviceServerController {
    @Value("${device.cashe_time}")
    private long cacheTime;
    @Autowired
    private JLInsideInsideService jlInsideInsideService;

    @PostMapping("/heartbeat")
    @ApiOperation(value = "巨龙设备心跳", notes = "设备对接")
    public ResultBean heartbeat(HttpServletRequest request, HttpServletResponse response) throws Exception {
        RequestWrapper requestWrapper = new RequestWrapper(request);
        String body = requestWrapper.getBody();
        JSONObject userJson = JSONObject.parseObject(body);
        JLHeartBean jlHeartBean = JSON.toJavaObject(userJson, JLHeartBean.class);
        jlInsideInsideService.heartbeat(null, jlHeartBean , cacheTime);
        return new ResultBean<>(1, "巨龙设备心跳", body);
    }

    @PostMapping("/distinguish")
    @ApiOperation(value = "人脸识别", notes = "人脸识别")
    public ResultBean distinguish(HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException {
        RequestWrapper requestWrapper = new RequestWrapper(request);
        String body = requestWrapper.getBody();
        jlInsideInsideService.compared(null,null,body);
        return new ResultBean<>(1, "人脸识别", body);
    }

}
